<?php
/**
 * 快递100
 */
namespace base\kuaidi\kuaidi100;
use help\HttpHelp;
use think\facade\Cache;

//$kuaidi = new \base\kuaidi\kuaidi100\Kuaidi100;
//$res =  $kuaidi->query('zhongtong','75803861936342');
		
class Kuaidi100 {
	
    //实时查询物流信息
    const API_URL_QUERY = 'http://poll.kuaidi100.com/poll/query.do';
    //查询轨迹地图
    const API_URL_MAPTRACK = 'http://poll.kuaidi100.com/poll/maptrack.do';
   
    //授权信息
    private $key;
    private $customer;
    
    //初始配置
    public function __construct($key='',$customer='')
    {
        $this->key = $key;
        $this->customer = $customer;
    }
    
	/*
    * 实时查询物流信息
    */
	public  function query($com ='',$num=''){
	    //参数设置
        $param = array (
            'com' => $com,             //快递公司编码
            'num' => $num,             //快递单号
            'phone' => '',                //手机号
            'from' => '',                   //出发地城市
            'to' => '',                       //目的地城市
            'resultv2' => '2'              //开启行政区域解析
        );
	    //授权
        $post_data = self::paramSing($param);
        $res = json_decode( HttpHelp::postRequest(self::API_URL_QUERY,$post_data) ,true);
        //结果
        if( !$res ){
            throw new \think\Exception( '快递请求异常错误！' );
        }
        if( $res['message'] != 'ok' ){
            throw new \think\Exception( $res['message'] );
        }
        return $res;
	}
	
    /*
    * 实时查询轨迹地图
    */
    public  function maptrack ($com ='',$num=''){
        $res = self::query($com,$num);
        
        //参数设置
        $param = array (
            'com' => $res['com'],                                   //快递公司编码
            'num' => $res['nu'],                                     //快递单号
            'phone' => '',                                              //手机号
            'from' =>$res['routeInfo']['from']['name'],   //出发地信息
            'to' => $res['routeInfo']['to']['name'] ?? $res['routeInfo']['cur']['name'],  //目的地信息
        );

        //授权
        $post_data = self::paramSing($param);
        $res = json_decode( HttpHelp::postRequest(self::API_URL_MAPTRACK,$post_data) ,true);
        //结果
        if( !$res ){
            throw new \think\Exception( '快递请求异常错误！' );
        }
        if( $res['message'] != 'ok' ){
            throw new \think\Exception( $res['message'] );
        }
        return $res;
    }
    
    /*
    * 获取json文件
    */
    public  function getkdbm($com ='',$num=''){
        $res = file_get_contents( base_path().'extend/base/kuaidi/kuaidi100/Kuaidi100.json' );
        return json_decode($res,true);
    }
    
    /*
    * 参数签名
    */
    private  function paramSing ($param){
        //授权
        $key = $this->key;                      //客户授权key
        $customer =  $this->customer;  //查询公司编号
        //请求参数
        $post_data = array();
        $post_data["customer"] = $customer;
        $post_data["param"] = json_encode($param);
        $post_data["sign"] = strtoupper( md5($post_data["param"].$key.$post_data["customer"]) );
        $params = "";
        foreach ($post_data as $k=>$v) {
            $params .= "$k=".urlencode($v)."&";              //默认UTF-8编码格式
        }
        $post_data = substr($params, 0, -1);
        return $post_data;
    }

    
}


?>